package command

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
	global "ssadmin/common/constant"
	"ssadmin/internal/admin/models"
	"strings"
	"time"
)

type DeptInsertReq struct {
	DeptId         string    `json:"-" comment:"部门id"` // 部门id
	ParentId       string    `json:"parentId" comment:"上级部门id"`
	DeptName       string    `json:"deptName" comment:"部门名称"`
	OrderNum       int32     `json:"orderNum" comment:"排序"`
	CreateTime     time.Time `json:"-" comment:"创建时间"`
	ModifyTime     time.Time `json:"-" comment:"修改时间"`
	CreatorId      string    `json:"-" comment:"创建者id"`
	Creator        string    `json:"-" comment:"创建者"`
	UpperCreatorId string    `json:"-" comment:"创建者的创建者"`
	Description    string    `json:"description" comment:"描述"`
	Type           string    `json:"type" comment:""`
}

func (s *DeptInsertReq) Bind(c *gin.Context) {
	s.Creator = user.Get(c, global.CacheKeyUserName).(string)
	s.CreatorId = user.Get(c, global.CacheKeyUserId).(string)
	s.UpperCreatorId = user.Get(c, global.CacheKeyCreatorId).(string)
}

func (s *DeptInsertReq) Generate(model *models.Dept) {
	if len(s.DeptId) == 0 {
		s.DeptId = uuid.NewV4().String()
	}
	model.DeptId = s.DeptId
	model.ParentId = s.ParentId
	model.DeptName = s.DeptName
	model.OrderNum = s.OrderNum
	model.CreateTimeDts = time.Now()
	model.ModifyTimeDts = time.Now()
	model.CreatorId = s.CreatorId
	model.Creator = s.Creator
	model.Description = s.Description
	model.Type = s.Type
}

func (s *DeptInsertReq) GetId() interface{} {
	return s.DeptId
}

type DeptUpdateReq struct {
	DeptId         string `json:"id" comment:"部门id" vd:"len($)>0"` // 部门id
	ParentId       string `json:"parentId" comment:"上级部门id"`
	DeptName       string `json:"deptName" comment:"部门名称"`
	OrderNum       int32  `json:"orderNum" comment:"排序"`
	UpperCreatorId string `json:"-" comment:"创建者的创建者"`
	Description    string `json:"description" comment:"描述"`
	Type           string `json:"type" comment:""`
	CreatorId      string `json:"-" comment:"创建者id"`
	Creator        string `json:"-" comment:"创建者"`
}

func (s *DeptUpdateReq) Generate(model *models.Dept) {
	if len(s.DeptId) == 0 {
		model.DeptId = uuid.NewV4().String()
	} else {
		model.DeptId = s.DeptId
	}
	model.ParentId = s.ParentId
	model.DeptName = s.DeptName
	model.OrderNum = s.OrderNum
	model.ModifyTimeDts = time.Now()
	model.Description = s.Description
	model.Type = s.Type
}

func (s *DeptUpdateReq) GetId() interface{} {
	return s.DeptId
}

// DeptDeleteReq 功能删除请求参数
type DeptDeleteReq struct {
	Ids       string `json:"ids"`
	CreatorId string `json:"-"`
}

func (s *DeptDeleteReq) GetId() []string {
	ids := strings.Split(s.Ids, ",")
	return ids
}
